package com.hmall.item.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hmall.item.mapper.ItemMapper;
import com.hmall.item.pojo.Item;
import com.hmall.item.service.IItemService;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
;

@Service
public class ItemService extends ServiceImpl<ItemMapper, Item> implements IItemService {
    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Transactional
    @Override
    public void UpAndDownShelves(Long id,Integer status){
        Item item = this.getById(id);
        UpdateWrapper<Item> itemUpdateWrapper = new UpdateWrapper<>();
        itemUpdateWrapper.eq("id",id);
        itemUpdateWrapper.set("status",status);
        this.update(item,itemUpdateWrapper);

        //根据商品的上下架状态去判定routngkey
        String routingKey=status==1?"itemExchange.up":"itemExchange.down";

        rabbitTemplate.convertAndSend("itemExchange",routingKey,id);
    }
}

